package youtube.tv

import youtube.tv.authentication.AppUser;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

class GaeTagLib {
    static namespace = 'gae'

    def authenticationService
    UserService userService = UserServiceFactory.userService

    // Login/Logout capability
    def loginLogout = { attrs ->
        AppUser user = authenticationService.getUser()

        String reqUri = attrs['reqUri']
        if (user == null) { // Login
            out << '<span id="authLink"><strong><a href="' + userService.createLoginURL(reqUri) + '" title="Google Account required">Login</a></strong></span>'
        } else { // Logout
            User googleUser = userService.currentUser
            if (googleUser) {
                out << '<span id="authLink"><strong><a href="' + userService.createLogoutURL(reqUri) + '" title="Authentication powered by Google">Logout:</a></strong> '
                out << googleUser
                out << '</span>'
            } else {
                out << """<span id="authLink"><strong>${user.userId?.encodeAsHTML()}</strong></span>"""
            }
        }
    }
}